package cn.huiyunche.driver.service.impl;

import cn.huiyunche.base.service.constant.WmsUrlConstant;
import cn.huiyunche.base.service.enums.DWaybillTypeEnum;
import cn.huiyunche.base.service.framework.utils.HttpUtils;
import cn.huiyunche.base.service.interfaces.impl.TmsOrderCallHistoryServiceImpl;
import cn.huiyunche.base.service.mappers.DWaybillMapper;
import cn.huiyunche.base.service.mappers.ext.DWaybillViewMapper;
import cn.huiyunche.base.service.model.DWaybill;
import cn.huiyunche.base.service.utils.HttpRequestUtil;
import cn.huiyunche.base.service.vo.DWaybillChargebackVO;
import cn.huiyunche.driver.service.UpdateWaybillService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UpdateWaybillServiceImpl implements UpdateWaybillService {
    @Autowired
    private DWaybillViewMapper dWaybillViewMapper;

    @Autowired
    private DWaybillMapper dWaybillMapper;

    private static final Logger LOGGER = LoggerFactory.getLogger(UpdateWaybillServiceImpl.class);
    @Override
    public void updateWaybillType() {
        List<DWaybillChargebackVO> vins = dWaybillViewMapper.getVins(DWaybillTypeEnum.NOT_KEY.getValue());
        for (DWaybillChargebackVO dWaybillChargeback : vins) {
            Map<String,Object> params= new HashMap<>();
            params.put("vin",dWaybillChargeback.getVin());
            String string = JSONObject.toJSONString(params);
            String s = HttpRequestUtil.postJson(WmsUrlConstant.isKey,null, string, HttpUtils.socketTimeout);
            JSONObject jsonObject = JSONObject.parseObject(s);
            Boolean data = jsonObject.getBoolean("data");
            Integer code = jsonObject.getInteger("code");
            if (code==0) {
                if (data) {
                    DWaybill dWaybill = new DWaybill();
                    dWaybill.setId(dWaybillChargeback.getId());
                    dWaybill.setWaybillType(DWaybillTypeEnum.NORMAL.getValue());
                    dWaybillMapper.updateByPrimaryKeySelective(dWaybill);
                }
            }else{
                LOGGER.error("UpdateWaybillServiceImpl.updateWaybillType"+jsonObject.getString("message"));
            }
        }
    }
}
